angular.module('merchant', [])
    .controller('merchantCtrl', ['$scope', 'httpService', '$stateParams', 'alertmsg', function($scope, httpService, $stateParams, alertmsg) {
        var id = $stateParams.id;
        var pagenum = 1;
        $scope.ispage = false;

        $scope.type = 1;
        $scope.setType = function(type) {
            $scope.type = type;
            pagenum = 1;
            if (type == 1) {
                $scope.getServiceOrder(1);
            } else {
                $scope.getServiceComment(1);
            }
        };

        function setpage(data, page) {
            $scope.ispage = false;
            if(data && data.length != 0 && page.total_count > limit) {
                $scope.ispage = true;
            }
        }

        // 获取服务商个人资料
        $scope.getServicePersion = function() {
            httpService.query('web/users/' + id).then(function(res) {
                if (res.code == 200) {
                    $scope.service_user = res.user;
                }
            });
        };
        $scope.getServicePersion();

        // 获取服务商评价
        $scope.getServiceComment = function(page) {
            if (page) {
                pagenum = page;
            }
            httpService.query('web/users/' + id + '/comments', { page: pagenum, limit: limit }).then(function(res) {
                if (res.code == 200) {
                    setpage(res.comments, res.page);
                    $scope.service_comments = res.comments;
                    $scope.page = res.page;
                } else {
                    alertmsg(res.msg);
                }
            });
        };

        // 获取服务商订单
        $scope.getServiceOrder = function(page) {
            if (page) {
                pagenum = page;
            }
            httpService.query('web/users/' + id + '/service_orders', { page: pagenum, limit: limit }).then(function(res) {
                if (res.code == 200) {
                    setpage(res.orders, res.page);
                    $scope.service_orders = res.orders;
                    $scope.page = res.page;
                } else {
                    alertmsg(res.msg);
                }
            });
        };
        $scope.getServiceOrder(1);

        // 分页
        $scope.getNext = function(page) {
            if ($scope.type == 1) {
                $scope.getServiceOrder(page);
            } else {
                $scope.getServiceComment(page);
            }
        };

        $scope.goBack = function() {
            window.history.go(-1);
        };

    }]);
